package com.gy.zpchat.ui.chat.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import im.zego.zim.callback.ZIMGroupMembersSearchedCallback;
import im.zego.zim.callback.ZIMGroupsSearchedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMGroupMemberInfo;
import im.zego.zim.entity.ZIMGroupMemberSearchConfig;
import im.zego.zim.entity.ZIMGroupSearchConfig;
import im.zego.zim.entity.ZIMGroupSearchInfo;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.ui.chat.group.adapter.GroupMemberSearchAdapter;
import com.gy.zpchat.ui.chat.group.adapter.GroupSearchAdapter;
import java.util.ArrayList;

public class ZIMGroupSearchResultsActivity
    extends Activity implements ZIMGroupsSearchedCallback, ZIMGroupMembersSearchedCallback {

    public static void actionStart(Activity activity, String groupID, String groupName,
                                   String searchKeyword) {
        Intent intent = new Intent(activity, ZIMGroupSearchResultsActivity.class);
        intent.putExtra("conversationID", groupID);
        intent.putExtra("conversationName", groupName);
        intent.putExtra("searchKeyword", searchKeyword);
        activity.startActivity(intent);
    }

    private static final String TAG = "ZIMGroupSearchActivity";
    private static final int SEARCH_COUNT = 5;
    private String conversationID;
    private String conversationName;
    private String searchKeyword;
    private int nextFlag = -1;

    private boolean isGroupSearch = false;

    private GroupMemberSearchAdapter groupMemberAdapter = new GroupMemberSearchAdapter();
    private GroupSearchAdapter groupGridAdapter = new GroupSearchAdapter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_search_results);
        searchKeyword = getIntent().getStringExtra("searchKeyword");
        conversationID = getIntent().getStringExtra("conversationID");
        conversationName = getIntent().getStringExtra("conversationName");
        if (conversationID == null) {
            isGroupSearch = true;
        }

        initView();
        if (isGroupSearch) {
            ZIMGroupSearchConfig config = new ZIMGroupSearchConfig();
            config.count = SEARCH_COUNT;
            config.isAlsoMatchGroupMemberNickname = true;
            config.isAlsoMatchGroupMemberUserName = true;
            config.keywords.add(searchKeyword);
            config.keywords.add("m");
            SDKManager.sharedInstance().getZim().searchLocalGroups(config, this);
        } else {
            ZIMGroupMemberSearchConfig config = new ZIMGroupMemberSearchConfig();
            config.count = SEARCH_COUNT;
            config.isAlsoMatchGroupMemberNickname = true;
            config.keywords.add(searchKeyword);
            SDKManager.sharedInstance().getZim().searchLocalGroupMembers(conversationID, config,
                                                                         this);
        }
    }

    private void initView() {
        RecyclerView recyclerView = findViewById(R.id.member_list);
        LinearLayoutManager linearLayoutManager =
            new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        if (isGroupSearch) {
            recyclerView.setAdapter(groupGridAdapter);
        } else {
            recyclerView.setAdapter(groupMemberAdapter);
        }

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition =
                    linearLayoutManager.findLastCompletelyVisibleItemPosition();
                int itemCount = 0;
                if (isGroupSearch) {
                    itemCount = groupGridAdapter.getItemCount();
                } else {
                    itemCount = groupMemberAdapter.getItemCount();
                }
                if (lastVisibleItemPosition == itemCount - 1) {
                    onLoadMsg();
                }
            }
        });
    }

    public void onLoadMsg() {
        if (nextFlag != 0) {
            if (isGroupSearch) {
                ZIMGroupSearchConfig config = new ZIMGroupSearchConfig();
                config.count = SEARCH_COUNT;
                config.nextFlag = nextFlag;
                config.isAlsoMatchGroupMemberNickname = true;
                config.isAlsoMatchGroupMemberUserName = true;
                config.keywords.add(searchKeyword);
                config.keywords.add("m");
                //  config.keywords.add("9");
                SDKManager.sharedInstance().getZim().searchLocalGroups(config, this);
            } else {
                ZIMGroupMemberSearchConfig config = new ZIMGroupMemberSearchConfig();
                config.count = SEARCH_COUNT;
                config.nextFlag = nextFlag;
                config.isAlsoMatchGroupMemberNickname = true;
                config.keywords.add(searchKeyword);
                SDKManager.sharedInstance().getZim().searchLocalGroupMembers(conversationID, config,
                                                                             this);
            }
        }
    }

    @Override
    public void onGroupsSearched(ArrayList<ZIMGroupSearchInfo> groupSearchInfoList, int nextFlag,
                                 ZIMError errorInfo) {

        for (ZIMGroupSearchInfo groupSearchInfo : groupSearchInfoList) {
            groupGridAdapter.addOrUpdate(groupSearchInfo);
        }
        this.nextFlag = nextFlag;
        groupGridAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGroupMembersSearched(String groupID, ArrayList<ZIMGroupMemberInfo> userList,
                                       int nextFlag, ZIMError errorInfo) {
        for (ZIMGroupMemberInfo groupMemberInfo : userList) {
            groupMemberAdapter.addOrUpdate(groupMemberInfo);
        }
        this.nextFlag = nextFlag;
        groupMemberAdapter.notifyDataSetChanged();
    }
}
